package com.qlogic.commons.platform.impls.lucene.indexer;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import com.qlogic.commons.StandardLogger;
import com.qlogic.commons.exceptions.BaseException;
import com.qlogic.commons.platform.impls.lucene.model.DocumentWrapper;
import com.qlogic.commons.platform.impls.lucene.model.RegistryRegion;

public class DefaultContentRegistry extends StandardLogger implements ContentRegistry {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 9093246848454292231L;
	
	private Hashtable regions = new Hashtable ();
	
	public Iterator getRegionNames () throws BaseException {
		return regions.keySet().iterator ();
	}
	
	public RegistryRegion getRegion (String regionName) throws BaseException {
		return (RegistryRegion)regions.get (regionName);
	}
	
	private RegistryRegion getRegion (String indexer, int action) {
		String regionName = indexer + action;
		RegistryRegion registryRegion = (RegistryRegion)regions.get (regionName);
		if (registryRegion == null) {
			registryRegion = new RegistryRegion ();
			registryRegion.setAction (action);
			registryRegion.setIndexer (indexer);
			regions.put (regionName, registryRegion);
		}
		return registryRegion;
	}
	
	public void registerForAdd(DocumentWrapper documentWrapper, String indexer) throws BaseException {
		RegistryRegion registryRegion = getRegion (indexer, ADD);
		registryRegion.addDocument (documentWrapper);
	}

	public void registerForAdd(Map documentWrappers, String indexer) throws BaseException {
		RegistryRegion registryRegion = getRegion (indexer, ADD);
		registryRegion.addDocuments (documentWrappers.values ());
	}

	public void registerForDelete(String contentId, String indexer) throws BaseException {
		RegistryRegion registryRegion = getRegion (indexer, DELETE);
		registryRegion.addDocument (new DocumentWrapper (contentId));
	}

	public void registerForDelete(String fieldName, String fieldValue, String indexer) throws BaseException {
		RegistryRegion registryRegion = getRegion (indexer, DELETE);
		DocumentWrapper documentWrapper = new DocumentWrapper ();
		documentWrapper.setFieldName (fieldName);
		documentWrapper.setFieldValue (fieldValue);
		registryRegion.addDocument (documentWrapper);
	}

	public void registerForUpdate(DocumentWrapper documentWrapper, String indexer) throws BaseException {
		documentWrapper.setDependsOn (indexer + DELETE);
		
		RegistryRegion deleteRegion = getRegion (indexer, DELETE);
		deleteRegion.addDocument (documentWrapper);
		
		RegistryRegion addRegion = getRegion (indexer, ADD);
		addRegion.addDocument (documentWrapper);
	}

	public String getLoggerName() {
		return "Default Content Registry";
	}

	public void setLoggerName(String name) {
	}

	public void unregister(RegistryRegion region, DocumentWrapper documentWrapper) throws BaseException {
		region.removeDocument (documentWrapper);
	}
	
	public void flush () throws BaseException {
		
	}

	
}
